#define DEBUG
#include <cstdio>

using namespace std;

const int maxp=500, maxr=10000, inf=0x7ffffff0;

class Graph {
public:
  int n, m, ind[maxp+1], to[maxr+1], link[maxr+1], dis[maxp+1][maxp+1], val[maxr+1][2];

  void addSide(int u, int v, int x, int y) {
    m++;
    to[m] = v;
    val[m][0]=x, val[m][1]=y;
    link[m] = ind[u];
    ind[u] = m;
  }

  void spfa(int s) {
    static int que[maxp*maxp+1][2];
    static bool book[maxp+1][maxp+1];
    for (int i=1; i<=n; i++) {
      for (int j=0; j<=n; j++) {
	dis[i][j] = inf;
	book[i][j] = 0;
      }
    }
    book[s][0] = true, que[1][0] = s, que[1][1] = 0 , dis[s][0] = 0;

    for (int h=0, t=1; h!=t; ) {
      h = h%(n*n)+1;
      int o=que[h][0], x=que[h][1];
      book[o][x] = false;
      for (int i = ind[o]; i; i = link[i]) {
	if (x+val[i][0]<=n && dis[to[i]][x+val[i][0]]>dis[o][x]+val[i][1]) {
	  dis[to[i]][x+val[i][0]] = dis[o][x]+val[i][1];
	  if (!book[to[i]][x+val[i][0]]) {
	    book[to[i]][x+val[i][0]] = true;
	    t = t%(n*n)+1;
	    que[t][0]=to[i], que[t][1]=x+val[i][0];
	  }
	}
      }
    }
  }
};

double getPos(int *a, int *b) {
  return (double)(a[1]-b[1])/(b[0]-a[0]);
}

int main() {
#ifdef DEBUG
  freopen("2.in", "r", stdin);
  freopen("2.out", "w", stdout);
#endif

  static Graph g;
  int p, r;
  scanf("%d %d", &p, &r);
  g.n = p;
  for (int i=1; i<=r; i++) {
    int c, d;
    scanf("%d %d ", &c, &d);
    char x=getchar();
    if (x!='x') {
      ungetc(x, stdin);
      int t;
      scanf("%d", &t);
      g.addSide(c, d, 0, t);
    } else {
      g.addSide(c, d, 1, 0);
    }
  }

  int q;
  scanf("%d", &q);
  for (int i=1; i<=q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    g.spfa(a);

    static int que[maxp+1][2];
    int size=0;
    for (int j=p; j>=0; j--) {
      if (g.dis[b][j]<inf) {
	if (size) {
	  int temp[2] = {j, g.dis[b][j]};
	  for (; (size>1 && getPos(que[size-1], que[size])>=getPos(que[size-1], temp)) || (size>0 && temp[0]<que[size][0] && temp[1]<=que[size][1]); size--);
	}
	size++;
	que[size][0]=j;
	que[size][1]=g.dis[b][j];
      }
    }

    if (!size) {
      printf("0 0\n");
      continue;
    } else if (g.dis[b][0]>=inf) {
      printf("inf\n");
      continue;
    }
    long long ans1=0, ans2=0;
    for (int j=1; j<size; j++) {
      int a, b, n;
      if (j==1) {
	a=que[1][1]+que[1][0];
	n=(que[2][1]-que[1][1])/(que[1][0]-que[2][0]);
      } else {
	a=((que[j-1][1]-que[j][1])/(que[j][0]-que[j-1][0])+1)*que[j][0]+que[j][1];
	n=(que[j+1][1]-que[j][1])/(que[j][0]-que[j+1][0])-(que[j-1][1]-que[j][1])/(que[j][0]-que[j-1][0]);
      }
      b=(que[j+1][1]-que[j][1])/(que[j][0]-que[j+1][0])*que[j][0]+que[j][1];
      ans2 += (long long)(a+b)*n/2;
      ans1 += n;
    }
    if (size<=1 || (que[size][1]-que[size-1][1])%que[size-1][0]) {
      ans1+=1, ans2+=que[size][1];
    }
    printf("%lld %lld\n", ans1, ans2);
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
